import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 3489309323@qq.com
 * @create 2020-09-14-23:43
 */
public class p4两任务有一个完成就立即执行第三个 {
  public static ExecutorService executor = Executors.newFixedThreadPool(10);

  public static void main(String[] args) throws ExecutionException, InterruptedException {
    /** runAfterEither/runAfterEitherAsync*/
    /*System.out.println("main...start...");

    CompletableFuture<Integer> future01 = CompletableFuture
      .supplyAsync(() -> {
        System.out.println("任务1线程开始");
        int i = 10 / 2;
        //int i = 10 / 0;
        System.out.println("运行结果: " + i);
        try {
          Thread.sleep(500);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        System.out.println("任务1线程结束");
        return i;
      }, executor);

    CompletableFuture<String> future02 = CompletableFuture
      .supplyAsync(() -> {
        System.out.println("任务2线程开始");
        System.out.println("任务2线程结束");
        return "Hello";
      }, executor);

    future01.runAfterEitherAsync(future02, () -> {
      System.out.println("任务3执行");
    });

    System.out.println("main...end...");*/
/*
如果是 runAfterEitherAsync
main...start...
任务1线程开始
运行结果: 5
任务2线程开始
任务2线程结束
main...end...
任务3执行
任务1线程结束

如果是 runAfterEither
main...start...
任务1线程开始
运行结果: 5
任务2线程开始
任务2线程结束
任务3执行
main...end...
任务1线程结束
* */


    /** acceptEither/acceptEitherAsync*/
    /** 前两个任务的返回值类型要一致 */
    /*System.out.println("main...start...");

    CompletableFuture<Object> future01 = CompletableFuture
      .supplyAsync(() -> {
        System.out.println("任务1线程开始");
        int i = 10 / 2;
        //int i = 10 / 0;
        System.out.println("运行结果: " + i);
        try {
          Thread.sleep(500);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        System.out.println("任务1线程结束");
        return i;
      }, executor);

    CompletableFuture<Object> future02 = CompletableFuture
      .supplyAsync(() -> {
        System.out.println("任务2线程开始");
        System.out.println("任务2线程结束");
        return "Hello";
      }, executor);

    future01.acceptEitherAsync(future02, (res) -> {
      System.out.println("任务3执行 ... 之前的结果是: "+ res);
    },executor);

    System.out.println("main...end...");*/
/*
main...start...
任务1线程开始
运行结果: 5
任务2线程开始
任务2线程结束
main...end...
任务3执行 ... 之前的结果是: Hello
任务1线程结束
* */

    /** applyToEither/applyToEitherAsync*/
    System.out.println("main...start...");

    CompletableFuture<Object> future01 = CompletableFuture
      .supplyAsync(() -> {
        System.out.println("任务1线程开始");
        int i = 10 / 2;
        //int i = 10 / 0;
        System.out.println("运行结果: " + i);
        try {
          Thread.sleep(500);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        System.out.println("任务1线程结束");
        return i;
      }, executor);

    System.out.println("-------");

    CompletableFuture<Object> future02 = CompletableFuture
      .supplyAsync(() -> {
        System.out.println("任务2线程开始");
        System.out.println("任务2线程结束");
        return "Hello";
      }, executor);

    CompletableFuture<String> future03 = future01.applyToEitherAsync(future02, (res) -> {
      System.out.println("任务3执行 ... 之前的结果是: " + res);
      return "res3";
    }, executor);

    System.out.println(future03.get());
    System.out.println("main...end...");
/*
main...start...
-------
任务1线程开始
运行结果: 5
任务2线程开始
任务2线程结束
任务3执行 ... 之前的结果是: Hello
res3
main...end...
任务1线程结束
* */
  }
}
